package com.security.manage.user.util;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class EntityUtil {
    //转换实体类
    public static <T> List<T> castEntity(List<Object[]> list, Class<T> clazz) throws Exception {
        List<T> returnList = new ArrayList<T>();
        Field[] fields = clazz.getDeclaredFields();
        Object[] co = new Object[fields.length];
        Class[] c2 = new Class[co.length];

        //确定构造方法
        for (int i = 0; i < co.length; i++) {
            co[i] = fields[i].getType();
            // 获取对应属性类型对应class
            Class<?> classs=Class.forName(co[i].toString().substring(6, co[i].toString().length()));
            c2[i] = classs;
        }

        for (Object[] o : list) {
            Constructor<T> constructor = clazz.getConstructor(c2);
            returnList.add(constructor.newInstance(o));
        }

        return returnList;
    }

}
